<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!function_exists('_set_error_message')){
    function _set_error_message($data = '')
	{
	   $ci = &get_instance();
	   if(empty($data))
            return false;
	   $str = '<div class="alert alert-error"><h4 class="alert-heading">Error!</h4>'.$data.'</div>';
	   $ci->session->set_flashdata('_message',$str);
       $ci->session->set_flashdata('_type','error');
    }
}
if(!function_exists('_set_success_message')){
    function _set_success_message($data = '')
	{
	   if(empty($data))
            return false;
	   $str = '<div class="alert alert-success"><h4 class="alert-heading">Success!</h4>'.$data.'</div>';
	   $ci = &get_instance();
       $ci->session->set_flashdata('_message',$str);
       $ci->session->set_flashdata('_type','success');
    }
}
if(!function_exists('_get_header_banner')){
    function _get_header_banner(){
        $ci = &get_instance();
        
        $ci->load->model('categories');
        $ci->load->model('articles');
        $cat = $ci->categories->getCat(array('cat_type'=>'header'));
        if(!empty($cat))
            return $ci->articles->get_article_by_category($cat[0]->cat_id);
        return null;
    }
}
if(!function_exists('_prices')){
    function _prices()
	{
	  
	   $ci = &get_instance();
       $str = '<li>'.anchor('tags/price/0-500',$ci->lang->line('over500'),'title="'.$ci->lang->line('over500').'"').'</li>';
        $str .= '<li>'.anchor('tags/price/500-1000',$ci->lang->line('over1000'),'title="'.$ci->lang->line('over1000').'"').'</li>';
        $str .= '<li>'.anchor('tags/price/1000-1500',$ci->lang->line('over1500'),'title="'.$ci->lang->line('over1500').'"').'</li>';
        $str .= '<li>'.anchor('tags/price/1500-2000',$ci->lang->line('over2000'),'title="'.$ci->lang->line('over2000').'"').'</li>';
        $str .= '<li>'.anchor('tags/price/2000',$ci->lang->line('over3000'),'title="'.$ci->lang->line('over3000').'"').'</li>';
       return $str;
    }
}
if(!function_exists('_create_recaptcha')){
function _create_recaptcha()
	{
	   $ci = &get_instance();	   
		$ci->load->helper('recaptcha');

		// Add custom theme so we can get only image
		$options = "<script>var RecaptchaOptions = {theme: 'custom', custom_theme_widget: 'recaptcha_widget'};</script>\n";

		// Get reCAPTCHA JS and non-JS HTML
		$html = recaptcha_get_html($ci->config->item('recaptcha_public_key', 'tank_auth'));

		return $options.$html;
	}
 }
 
 function _check_recaptcha()
	{
	   $ci = &get_instance();
		$ci->load->helper('recaptcha');

		$resp = recaptcha_check_answer($ci->config->item('recaptcha_private_key', 'tank_auth'),
				$_SERVER['REMOTE_ADDR'],
				$_POST['recaptcha_challenge_field'],
				$_POST['recaptcha_response_field']);

		if (!$resp->is_valid) {
		///	$this->form_validation->set_message('_check_recaptcha', $this->lang->line('auth_incorrect_captcha'));
			return FALSE;
		}
		return TRUE;
	}
if(!function_exists('_upload')){
function _upload($config=array(),$filename='userfile')
{
     $ci = &get_instance();
    $POST_MAX_SIZE = $config['max_size'];
    $unit = strtoupper(substr($POST_MAX_SIZE, -1));
    $multiplier = ($unit == 'M' ? 1048576 : ($unit == 'K' ? 1024 : ($unit == 'G' ? 1073741824 : 1)));
    $error = array();
    if ((int) $_SERVER['CONTENT_LENGTH'] > $multiplier * (int) $POST_MAX_SIZE && $POST_MAX_SIZE)
    {
        _set_error_message("POST exceeded maximum allowed size.");
         return false;
    }
    
    // Settings
    $save_path =  $config['upload_path'];//$_SERVER['DOCUMENT_ROOT'] .'/cdn/uploads/'. strtoupper(dechex(sprintf("%u", crc32(date('Y-m-d'))))) . '/';     // The path were we will save the file (getcwd() may not be reliable and should be tested in your environment)
    
    //Check dir exists
    if (!is_dir($save_path))
    {
    //Create dir
        if (mkdir($save_path))
        {
            //Grant permission
            chmod($save_path, 0777);
        } 
        else
        {
            _set_error_message("Cannot create folder.");
            return false;
        }
    }
    
    $upload_name = $filename;
    $max_file_size_in_bytes = 2147483647;    // 2GB in bytes
    $extension_whitelist = $config['allow_type']; // Allowed file extensions
    $valid_chars_regex = '.A-Z0-9_ !@#$%^&()+={}\[\]\',~`-';    // Characters allowed in the file name (in a Regular Expression format)
    // Other variables	
    $MAX_FILENAME_LENGTH = 260;
    $file_name = "";
    $file_extension = "";
    $uploadErrors = array(
    0 => "There is no error, the file uploaded successfully",
    1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
    2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
    3 => "The uploaded file was only partially uploaded",
    4 => "No file was uploaded",
    6 => "Missing a temporary folder"
    );
    
    
    // Validate the upload
    if (!isset($_FILES[$upload_name]))
    {
        _set_error_message("No upload found in \$_FILES for " . $upload_name);
        return false;
    }
    else if (isset($_FILES[$upload_name]["error"]) && $_FILES[$upload_name]["error"] != 0)
    {
        _set_error_message($uploadErrors[$_FILES[$upload_name]["error"]]);
       return false;
    }
    else if (!isset($_FILES[$upload_name]["tmp_name"]) || !@is_uploaded_file($_FILES[$upload_name]["tmp_name"]))
    {
        _set_error_message("Upload failed is_uploaded_file test.");
       return false;
    } 
    else if (!isset($_FILES[$upload_name]['name']))
    {
        _set_error_message("File has no name.");
        return false;
    }
    
    // Validate file extension
    $path_info = pathinfo($_FILES[$upload_name]['name']);
    $file_extension = $path_info["extension"];
    $is_valid_extension = false;
    foreach ($extension_whitelist as $extension)
    {
        if (strcasecmp($file_extension, $extension) == 0)
        {
            $is_valid_extension = true;
            break;
        }
    }
    
    if (!$is_valid_extension)
    {
        _set_error_message("Invalid file extension");
        return false;
    }
    
    // Validate the file size (Warning: the largest files supported by this code is 2GB)
    $file_size = @filesize($_FILES[$upload_name]["tmp_name"]);
    if (!$file_size || $file_size > $max_file_size_in_bytes)
    {
        _set_error_message("File exceeds the maximum allowed size");
        return false;
    }
    
    if ($file_size <= 0)
    {
        _set_error_message("File size outside allowed lower bound");
        return false;
    }
    
    
    // Validate file name (for our purposes we'll just remove invalid characters)
    $file_name = preg_replace('/[^' . $valid_chars_regex . ']|\.+$/i', "", basename($_FILES[$upload_name]['name']));
    
    if (strlen($file_name) == 0 || strlen($file_name) > $MAX_FILENAME_LENGTH)
    {
    _set_error_message("Invalid file name");
    return false;
    }
    
    $original_name = $file_name;
    
    $file_name = md5($file_name . rand() . microtime()) . '.' . $file_extension;
    
    // Validate that we won't over-write an existing file
    if (file_exists($save_path . $file_name))
    {
        _set_error_message("File with this name already exists");
        exit(0);
    }
    
    // Validate file contents (extension and mime-type can't be trusted)
    /*
    Validating the file contents is OS and web server configuration dependant.  Also, it may not be reliable.
    See the comments on this page: http://us2.php.net/fileinfo
    
    Also see http://72.14.253.104/search?q=cache:3YGZfcnKDrYJ:www.scanit.be/uploads/php-file-upload.pdf+php+file+command&hl=en&ct=clnk&cd=8&gl=us&client=firefox-a
    which describes how a PHP script can be embedded within a GIF image file.
    
    Therefore, no sample code will be provided here.  Research the issue, decide how much security is
    needed, and implement a solution that meets the need.
    */
    
    
    // Process the file
    /*
    At this point we are ready to process the valid file. This sample code shows how to save the file. Other tasks
    could be done such as creating an entry in a database or generating a thumbnail.
    
    Depending on your server OS and needs you may need to set the Security Permissions on the file after it has
    been saved.
    */
    if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.'/'. $file_name))
    {
        _set_error_message("File could not be saved.");
        return false;
    }
    
    return array(
    'error' => 0,
    'url' => str_replace('./..', '', $save_path .'/'. $file_name),
    'md5' => md5(file_get_contents(realpath($save_path .'/'. $file_name))),
    'hashed_name' => str_replace('.' . $file_extension, '', $file_name),
    'original_name' => $original_name,
    'size' => $file_size,
    'extension' => $file_extension
    );   
    
    /* Handles the error output. This error message will be sent to the uploadSuccess event handler.  The event handler
    will have to check for any error messages and react as needed. */
    
    
    }
}

if(!function_exists('_get_next_article')){
    function _get_next_article($post_id){
        $ci = &get_instance();
        $ci->db->where('cat_id',$ci->uri->rsegment(3));
        
        $ci->db->where('post_id >',$post_id);
        $ci->db->order_by('post_id');
        
        $post = $ci->db->get('posts')->row();
        if(!empty($post)){
        $alias = $ci->_global->_unserialize($post->seo_alias);
        $title = $ci->_global->_unserialize($post->seo_title);
        return anchor('gallery/c'.$post->cat_id.'/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$post->post_id,'Next','class="next" style="top:55%;right:-38px" ');
        }
    }
}
if(!function_exists('_get_prev_article')){
    function _get_prev_article($post_id){
        $ci = &get_instance();
        $ci->db->where('cat_id',$ci->uri->rsegment(3));
        $ci->db->where('post_id <',$post_id);
        $ci->db->order_by('post_id','desc');
        
        $post = $ci->db->get('posts')->row();
        if(!empty($post)){
        $alias = $ci->_global->_unserialize($post->seo_alias);
        $title = $ci->_global->_unserialize($post->seo_title);
        return anchor('gallery/c'.$post->cat_id.'/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$post->post_id,'Next','class="prev" style="top:55%;left:-1px" ');
        }
    }
}
if(!function_exists('_get_menu')){
    function _get_menu($parent_id = 0,$menuname = false)
	{
	   if($menuname) return false;
      // $menuList = $sep;
       $ci = &get_instance();
        global $menus;
       $items = array();
       $url = '';
       $ci->load->model('categories');
       $ci->load->model('articles');
       if(!empty($menus)){
           foreach($menus as $mn){
                $arr[$mn->cat_id] = $mn;
                $contact_pr ='';
                $home_pr='';
                if($mn->parent_id == $parent_id){
                    
                    $cid = $ci->uri->rsegment(3);
                    $pid = $ci->uri->rsegment(5);
                    $cid2 = $ci->uri->rsegment(1);
                    
                    if($mn->cat_id == 1 && $cid==false && $cid2!='contact'){
                        $home_pr = 'class="active"';
                        
                    }else{
                        $home_pr='';
                        
                    }
                    $name = $ci->_global->_unserialize($mn->cat_name);
                    $alias = $ci->_global->_unserialize($mn->cat_alias);
                    
                    if($mn->cat_id==$cid){
                        $home_pr = '';
                        $pr = 'class="active"';
                        
                    }else {$pr='';}
                   
                    if($mn->cat_type == 'article'){
                        if($mn->link!='')
                            $url =$mn->link;
                        else{
                        $c = $ci->categories->countArticleByCatId($mn->cat_id);
                        $p = count(_get_category_parent_id($mn->cat_id));
                        
                        if($c>1 || $p>0){
                            if(isset($alias[$ci->session->userdata('lan')]))
                        $url ='news/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$mn->cat_id;
                       
                        }
                        else{
                            if($c>0){
                            $article =$ci->articles->get_article_by_category($mn->cat_id);
                            if(!empty($article)){
                            $alias = $ci->_global->_unserialize($article[0]->seo_alias);
                            
                            if(isset($alias[$ci->session->userdata('lan')]))
                            $alias = $alias[$ci->session->userdata('lan')]['alias'];
                            else
                             $alias="";           
                            $url ='news/c'.$mn->cat_id.'/'.$alias.'-'.$article[0]->post_id;
                            }else{
                                $url = '';
                            }
                            }else
                               
                                    $url = '';
                            
                            }
                        }
                    }
                    elseif($mn->cat_type== 'shop'){
                        if(isset($alias[$ci->session->userdata('lan')]))
                        $url ='shop/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$mn->cat_id;
                        }
                    elseif($mn->cat_type=='gallery'){
                        $c = $ci->categories->countArticleByCatId($mn->cat_id);
                                                
                            if($c>0){
                            $article =$ci->articles->get_article_by_category($mn->cat_id);
                            if(!empty($article)){
                            $alias = $ci->_global->_unserialize(end($article)->seo_alias);
                            if(!empty($alias)){
                            if(isset($alias[$ci->session->userdata('lan')]))
                                $alias = $alias[$ci->session->userdata('lan')]['alias'];
                            else
                                $alias="";           
                            $url ='gallery/c'.$mn->cat_id.'/'.$alias.'-'.end($article)->post_id;
                            }else
                                $url='';
                            }
                            }else{
                                 $p = _get_category_parent_id($mn->cat_id,false);
                               
                               
                                if(count($p)>0){
                                    $p = end($p);
                                    $cat_alias = $ci->_global->_unserialize($p->cat_alias);
                                    $url='gallery/'.$cat_alias[$ci->session->userdata('lan')]['alias'].'-'.$p->cat_id;
                                }else
                                    $url = '';
                            }
                        }
                        
                        
                    
                    elseif($mn->cat_type=='contact'){
                        $url ='contact';
                        if($cid2=='contact')
                            $contact_pr = 'class="active"';
                        }
                        if(isset($name[$ci->session->userdata('lan')]))
                            $items[] = '<li>'.anchor($url,$name[$ci->session->userdata('lan')]['title'],$home_pr.$pr.$contact_pr.'id="item-'.$mn->cat_id.'" title="'.$name[$ci->session->userdata('lan')]['title'].'"')._get_menu($mn->cat_id).'</li>';
                    
                }
           }
       }
       if(count($items)>0){
            return '<ul>'.implode('', $items).'</ul>';
       }
       return "";       
	}
}



if(!function_exists('_get_menu_left')){
    function _get_menu_left($parent_id = 0,$type)
	{
	   
      // $menuList = $sep;
       $ci = &get_instance();
       
       $items = array();
       $url = "";
       $ci->load->model('categories');
       $categories = $ci->categories->getCat(array('cat_type'=>$type));
       
       $ci->load->model('articles');
       
       if(!empty($categories)){
           foreach($categories as $mn){
                $arr[$mn->cat_id] = $mn;
                if($mn->parent_id == $parent_id){
                    
                    $name = $ci->_global->_unserialize($mn->cat_name);
                    $alias = $ci->_global->_unserialize($mn->cat_alias);
                    
                    if($mn->parent_id<1 ) $class='class="parent"';
                    else $class ='class="subitem"';
                    if($mn->cat_type == 'article'){
                        $c = $ci->categories->countArticleByCatId($mn->cat_id);
                        if($c>1){
                            if(isset($alias[$ci->session->userdata('lan')]))
                        $url ='news/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$mn->cat_id;
                       
                        }
                        else{
                            if($c>0){
                            $article =$ci->articles->get_article_by_category($mn->cat_id);
                            
                            $alias = $ci->_global->_unserialize($article[0]->seo_alias);
                            
                            if(isset($alias[$ci->session->userdata('lan')]))
                            $alias = $alias[$ci->session->userdata('lan')]['alias'];
                            else
                             $alias="";           
                            $url ='news/c'.$mn->cat_id.'/'.$alias.'-'.$article[0]->post_id;
                            }else{
                                $url="";
                            }
                        }
                    }
                    elseif($mn->cat_type== 'shop'){
                        if(isset($alias[$ci->session->userdata('lan')]))
                        $url ='shop/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$mn->cat_id;
                        }
                    elseif($mn->cat_type=='contact')
                        $url ='contact';
                        
                        if(isset($name[$ci->session->userdata('lan')]))
                            $items[] = '<li '.$class.'>'.anchor($url,$name[$ci->session->userdata('lan')]['title'],'id=menu-'.$mn->cat_id)._get_menu_left($mn->cat_id,$type).'</li>';
                }
           }
       }
       if(count($items)>0){
            return '<ul>'.implode('', $items).'</ul>';
       }
       return "";       
	}
}

if(!function_exists('_get_country')){
    function _get_country($parent_id=0,$i=0){
      // $menuList = $sep;
       $ci = &get_instance();
       
       $items = array();
       $url = "";
      
       $categories = $ci->db->get('countries')->result();
     
      
       
       if(!empty($categories)){
           foreach($categories as $mn){
                
                if($mn->parent_id == $parent_id){
                    
                    if($mn->parent_id<1 ){ $class='class=""';$sep='';}
                    else{ $class ='class="cat_subitem"';$sep='|_';}
                    
                    $px = 20*$i;
                       
                            $items[] = '<tr><td width=82%>'.anchor('',$sep.$mn->name,'style="padding-left:'.$px.'px"').'</td><td>'.anchor('administrator/setting/country/delete/'.$mn->id,'<i class="icon-remove"></i> Delete', 'class="btn action" onclick="return confirm(\'Do you want to delete?\')"').'</td></tr>'._get_country($mn->id,$i+1);
                    
                }
                
                
           }
       }
       
       if(count($items)>0){
            return implode('', $items);
       }
       return ""; 
    }
}

if(!function_exists('_get_country2')){
    function _get_country2($parent_id=0,$i=0){
      // $menuList = $sep;
       $ci = &get_instance();
       
       $items = array();
       $url = "";
      
       $categories = $ci->db->get('countries')->result();
     
      
       
       if(!empty($categories)){
           foreach($categories as $mn){
                
                if($mn->parent_id == $parent_id){
                    
                    
                    $px= '';
                    if($mn->parent_id<1 ){ $class='class=""';$sep='';}
                    else{ $class ='class="cat_subitem"';$sep='+';}
                    $px .= $sep;
                            $items[] = '<option value="'.$mn->id.'">'.$sep.$mn->name.'</span></option>'._get_country2($mn->id,$i+1);
                    
                }
                
                
           }
       }
       
       if(count($items)>0){
            return implode('', $items);
       }
       return ""; 
    }
}
if(!function_exists('_get_relative_post')){
    function _get_realtive_post($cat_id,$post_id){
        $ci = &get_instance();
        $sql = "select * from posts where valid=1 and cat_id ={$cat_id} and post_id <> {$post_id} ORDER BY RAND() LIMIT 5";
        $query = $ci->db->query($sql);
        if(!empty($query))
            return $query->result();
        return false;
    }
}
if(!function_exists('_get_country3')){
    function _get_country3($parent_id=0){
      // $menuList = $sep;
       $ci = &get_instance();
       
       $items = '';
      $ci->db->where('parent_id',$parent_id);
       $categories = $ci->db->get('countries')->result();
     
      
       
       if(!empty($categories)){
           foreach($categories as $mn){
                
                if($mn->parent_id == $parent_id){
                    
                            $items.='<option value="'.$mn->id.'">'.$mn->name.'</option>';
                    
                }
           }
       }
       
       return $items; 
    }
}

if(!function_exists('_get_sitemap')){
    function _get_sitemap($parent_id=0,$i=0){
      // $menuList = $sep;
       $ci = &get_instance();
       
       $items = array();
       $url = "";
       $ci->load->model('categories');
       $categories = $ci->categories->getCat(array('valid'=>0));
       
      
       
       if(!empty($categories)){
           foreach($categories as $mn){
                
                if($mn->parent_id == $parent_id){
                    
                    if($mn->parent_id<1 ){ $class='class=""';$sep='';}
                    else{ $class ='class="cat_subitem"';$sep='|_';}
                    
                    $name = $ci->_global->_unserialize($mn->cat_name);
                    $alias = $ci->_global->_unserialize($mn->cat_alias);
                    
                    
                    if($mn->cat_type == 'article'){
                        if($mn->link!='')
                            $url =$mn->link;
                        else{
                        $c = $ci->categories->countArticleByCatId($mn->cat_id);
                        $p = count(_get_category_parent_id($mn->cat_id));
                        
                        if($c>1 || $p>0){
                            if(isset($alias[$ci->session->userdata('lan')]))
                        $url ='news/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$mn->cat_id;
                       
                        }
                        else{
                            if($c>0){
                            $article =$ci->articles->get_article_by_category($mn->cat_id);
                            
                            $alias = $ci->_global->_unserialize($article[0]->seo_alias);
                            
                            if(isset($alias[$ci->session->userdata('lan')]))
                            $alias = $alias[$ci->session->userdata('lan')]['alias'];
                            else
                             $alias="";           
                            $url ='news/c'.$mn->cat_id.'/'.$alias.'-'.$article[0]->post_id;
                            }else{
                                $url = '';
                            }
                        }
                        }
                    }
                    elseif($mn->cat_type== 'shop'){
                        if(isset($alias[$ci->session->userdata('lan')]))
                        $url ='shop/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$mn->cat_id;
                        }
                    elseif($mn->cat_type=='gallery'){
                        $c = $ci->categories->countArticleByCatId($mn->cat_id);
                        
                            if($c>0){
                            $article =$ci->articles->get_article_by_category($mn->cat_id);
                            
                            $alias = $ci->_global->_unserialize($article[0]->seo_alias);
                            
                            if(isset($alias[$ci->session->userdata('lan')]))
                            $alias = $alias[$ci->session->userdata('lan')]['alias'];
                            else
                             $alias="";           
                            $url ='gallery/c'.$mn->cat_id.'/'.$alias.'-'.$article[0]->post_id;
                            }else{
                                $url = '';
                            }
                        }
                        
                        
                    
                    elseif($mn->cat_type=='contact')
                        $url ='contact';
                    $px = 20*$i;
                    $px2 =40*$i;
                    $articles = _get_posts($mn->cat_id);
                    $s = '';
                    if(!empty($articles)){
                        
                        foreach($articles as $article){
                            $post_title = $ci->_global->_unserialize($article->post_title);
                            $post_title = $post_title[$ci->session->userdata('lan')]['post_title'];
                            $post_alias = $ci->_global->_unserialize($article->seo_alias);
                            $post_alias = $post_alias[$ci->session->userdata('lan')]['alias'];
                            $url2='';
                            if($mn->cat_type=='new')
                                $url2 ='news/c'.$mn->cat_id.'/'.$post_alias.'-'.$article->post_id;
                            if($mn->cat_type=='gallery')
                                $url2 ='gallery/c'.$mn->cat_id.'/'.$post_alias.'-'.$article->post_id;
                            
                            $s.='<tr><td width=82%>'.anchor($url2,$sep.$post_title,'style="padding-left:'.$px2.'px" title="'.$post_title.'"').'</td></tr>';
                        }
                    }
                        if(isset($name[$ci->session->userdata('lan')]))
                            $items[] = '<tr><td width=82%>'.anchor($url,$sep.$name[$ci->session->userdata('lan')]['title'],'style="padding-left:'.$px.'px"  title="'.$name[$ci->session->userdata('lan')]['title'].'"').'</td></tr>'.$s._get_sitemap($mn->cat_id,$i+1);
                   
                    
                }
                
                
                
                
           }
       }
       if(count($items)>0){
            return implode('', $items);
       }
       return ""; 
    }
}

if(!function_exists('_get_tree_categories')){
    function _get_tree_categories($parent_id=0,$cat_type=false,$i=0){
      // $menuList = $sep;
       $ci = &get_instance();
       
       $items = array();
       $url = "";
       $ci->load->model('categories');
       if($cat_type)
        $categories = $ci->categories->getCat(array('cat_type'=>$cat_type));     
       else
        $categories = $ci->categories->getCat();
       if(!empty($categories)){
           foreach($categories as $mn){
                
                if($mn->parent_id == $parent_id){
                    
                    if($mn->parent_id<1 ){ $class='class=""';$sep='';}
                    else{ $class ='class="cat_subitem"';$sep='|_';}
                    
                    $name = $ci->_global->_unserialize($mn->cat_name);
                    $alias = $ci->_global->_unserialize($mn->cat_alias);
                    $px = 20*$i;
                        if(isset($name[$ci->session->userdata('lan')]))
                            $items[] = '<tr><td width=82%>'.anchor($url,$sep.$name[$ci->session->userdata('lan')]['title'],'style="padding-left:'.$px.'px"').'</td><td>'.anchor('administrator/category/index/'.$mn->cat_type.'/edit/'.$mn->cat_id,'<i class="icon-edit"></i> Edit', 'class="btn action"').'</td><td>'.anchor('administrator/category/index/'.$mn->cat_type.'/delete/'.$mn->cat_id,'<i class="icon-remove"></i> Delete', 'class="btn action" onclick="return confirm(\'Do you want to delete this Category?\')"').'</td></tr>'._get_tree_categories($mn->cat_id,$cat_type,$i+1);
                   
                    
                }
                
                
                
                
           }
       }
       if(count($items)>0){
            return implode('', $items);
       }
       return ""; 
    }
}

if(!function_exists('_get_tree_categories')){
    function _get_tree_categories($parent_id=0,$i=0){
      // $menuList = $sep;
       $ci = &get_instance();
       
       $items = array();
       $url = "";
       $ci->load->model('categories');
       $categories = $ci->categories->getCat();
       
      
       
       if(!empty($categories)){
           foreach($categories as $mn){
                
                if($mn->parent_id == $parent_id){
                    
                    if($mn->parent_id<1 ){ $class='class=""';$sep='';}
                    else{ $class ='class="cat_subitem"';$sep='|_';$i++;}
                    
                    $name = $ci->_global->_unserialize($mn->cat_name);
                    $alias = $ci->_global->_unserialize($mn->cat_alias);
                    $px = 20*$i;
                        if(isset($name[$ci->session->userdata('lan')]))
                            $items[] = '<tr><td width=82%>'.anchor($url,$sep.$name[$ci->session->userdata('lan')]['title'],'style="padding-left:'.$px.'px"').'</td><td>'.anchor('administrator/category/index/edit/'.$mn->cat_id,'<i class="icon-edit"></i> Edit', 'class="btn action"').'</td><td>'.anchor('administrator/category/index/delete/'.$mn->cat_id,'<i class="icon-remove"></i> Delete', 'class="btn action" onclick="return confirm(\'Do you want to delete this Category?\')"').'</td></tr>'._get_tree_categories($mn->cat_id,$i);
                    $i=0;
                }
                
                
           }
       }
       if(count($items)>0){
            return implode('', $items);
       }
       return ""; 
    }
}

if(!function_exists('_get_product')){
    function _get_product($cat_id=false,$limited=false){
        if(!$cat_id) return false;
        $ci = &get_instance();
        $ci->load->model('products');
        $products = $ci->products->getProductCategory($cat_id,$limited);
        return $products;
    }
}
if(!function_exists('_get_cat_by_id')){
    function _get_cat_by_id($cat_id=false){
        if(!$cat_id) return false;
        $ci = &get_instance();
        $ci->load->model('categories');
        $category = $ci->categories->getCat(array('cat_id'=>$cat_id));
        return $category->cat_name;
    }
}
if(!function_exists('_get_product_by_cat')){
    function _get_product_by_cat($cat_id=false,$parent_id=false,$limited=false){
        if(!$cat_id) return false;
        $ci = &get_instance();
        $ci->db->where('cat_id',$cat_id);
        if($parent_id>0){
            $ci->db->or_where('cat_id',$parent_id);
        }
        $products = $ci->db->get('products')->result();
        return $products;
    }
}
if(!function_exists('_get_detail_product')){
    function _get_detail_product($product_id=false){
        if(!$product_id) return false;
        $ci = &get_instance();
        $ci->load->model('products');
        $product = $ci->products->getProduct(array('product_id'=>$product_id));
        return $product;
    }
}
if(!function_exists('_get_metadata')){
    function _get_metadata(){
        global $site_info;
       $str='<title>'.$site_info['site_title'].'</title>';
        $str.='<meta name="description" content="'.$site_info['metadesc'].'" />';
        $str.='<meta name="keywords" content="'.$site_info['metakey'].'" />';
        $str.='<meta name="robots" content="index, follow" />';
        $str.='<meta name="generator" content="BW CMS - http://cuongpham.info" />';
        $str.='<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
        return $str;
    }
}
if(!function_exists('_get_related_products')){
    function _get_related_products(){
        $ci = &get_instance();
        $cat_id = $ci->uri->rsegment(3);
        $sql = "select * from products where cat_id='".$cat_id."' and valid=0 ORDER BY RAND() limit 4";
        $query = $ci->db->query($sql);
        if($query->num_rows()>0)
            return $query->result();
    }
}

if(!function_exists('_get_shop_categories')){
    function _get_shop_categories($parent_id=0){
        $ci=&get_instance();
        $str = "";
        $ci->load->model('categories');
        $categories = $ci->categories->getCat(array('cat_type'=>'shop'));
        foreach($categories as $cat){
            if($cat->parent_id == $parent_id){
                if($cat->parent_id>0) $class='class="subcat"';
                else $class ='';
                $alias = $ci->_global->_unserialize($cat->cat_alias);
               
                if(isset( $alias[$ci->session->userdata('lan')]))
                    $alias = $alias[$ci->session->userdata('lan')]['alias'];
                else
                    $alias = '';
                $title = $ci->_global->_unserialize($cat->cat_name);
                if(isset( $title[$ci->session->userdata('lan')]))
                    $title = $title[$ci->session->userdata('lan')]['title'];
                else
                    $title = '';
                $str.='<li '.$class.'>'.anchor('shop/'.$alias.'-'.$cat->cat_id,$title,'title="'.$title.'"').'</li>';
                $str.=_get_shop_categories($cat->cat_id);
                unset($title,$alias);
            }
        }
        unset($categories);
        return $str;
    }
}



if(!function_exists('_insert_tags')){
    function _insert_tags($tagline,$type=''){
        $ci = &get_instance();
        $ci->db->where('tag_type',$type);
        $tags = $ci->db->get('tags')->result_array();
      //  var_dump($tags);
        if(!empty($tags)){
            
            foreach($tags as $key=>$val){
                $arr[] = $val['name'];
            }
            if(!in_array(strtolower($tagline),$arr))
                $ci->db->insert('tags',array('name'=>strtolower($tagline),'tag_type'=>$type));
        }else
            $ci->db->insert('tags',array('name'=>strtolower($tagline),'tag_type'=>$type));        
    }
}
if(!function_exists('_search_tags')){
    function _search_tags($tagline,$page=false,$show=10){
        if($page!==false)
            $sql="select * from posts where valid=1 and find_in_set('{$tagline}',tagline) > 0 limit $page,$show";
         else
             $sql="select * from posts where valid=1 and find_in_set('{$tagline}',tagline) > 0";
        $ci=&get_instance();
        $query = $ci->db->query($sql);
        if($query->num_rows()>0)
            return $query->result();
        return false;
    }
}
if(!function_exists('_get_posts')){
    function _get_posts($id = false,$post_id = false,$limited=false,$type=false,$page=5){
        $ci=&get_instance();
        $ci->load->model('articles');
        if(!$id) {
          $posts = $ci->articles->get_article_by_category(false,$limited,$page,$type); 
            
        }elseif(!$post_id && $id)
            $posts = $ci->articles->get_article_by_category($id,$limited,$page,$type);

        else
            $posts = $ci->articles->get_article($post_id);
        if(!empty($posts)){
            return $posts;
        }
        return false;
    }
}
if(!function_exists('_get_gallery')){
    function _get_gallery($id = false,$post_id = false,$limited=false){
        $ci=&get_instance();
        $ci->load->model('articles');
        if($id) {
          $posts = $ci->articles->get_article_by_category($id,false,20,'gallery'); 
          
        }elseif($post_id)
            $posts = $ci->articles->get_article($post_id);
        
        if(!empty($posts)){
            return $posts;
        }
        return false;
    }
}
if(!function_exists('_get_profile')){
    function _get_profile($id = false,$post_id = false,$limited=false){
        $ci=&get_instance();
        $ci->load->model('articles');
        if($id) {
          $posts = $ci->articles->get_article_by_category($id,false,20,'profile'); 
          
        }elseif($post_id)
            $posts = $ci->articles->get_article($post_id);
        
        if(!empty($posts)){
            return $posts;
        }
        return false;
    }
}

if(!function_exists('_check_type')){
    function _check_type($cat_id){
        $ci=&get_instance();
        $ci->db->where('cat_id',$cat_id);
        $r = $ci->db->get('categories')->row();
        return $r->cat_type;
    }
}
if(!function_exists('_breadcrumb')){
    function _breadcrumb($parent_id){
        $ci=&get_instance();
        if($parent_id>0){
        $ci->db->where('cat_id',$parent_id);
        $ci->db->or_where('parent_id',$parent_id);
        $r = $ci->db->get('categories')->result();
        if(!empty($r)){
           foreach($r as $rs){
            $data[$rs->cat_id] = $rs;
           }
           return $data;
        }
        }
        return null;
    }
}
if(!function_exists('_add_hit')){
    function _add_hit($post_id){
        $ci=&get_instance();
        $sql = "update posts set hit = hit+1 where post_id=$post_id";
        $ci->db->query($sql);
    }
}
if(!function_exists('_get_hits')){
    function _get_hits(){
        $ci=&get_instance();
        $ci->db->limit(3);
        $ci->db->where('type','article');
        $ci->db->order_by('hit','desc');
        return $ci->db->get('posts')->result();
    }
}
if(!function_exists('_get_posts2')){
    function _get_posts2($id = false,$post_id = false,$limited=false){
        if(!$id) return false;
        $ci=&get_instance();
        $ci->load->model('articles');
        if(!$post_id)
            $posts = $ci->articles->get_article_by_category($id,0,4);
        else
            $posts = $ci->articles->get_article($post_id);
        if(!empty($posts)){
            return $posts;
        }
        return false;
    }
}
if(!function_exists('_get_comments')){
    function _get_comments(){
        $ci=&get_instance();
        $ci->load->model('comment');
        $comments = $ci->comment->getComment($ci->uri->rsegment(5),1);
        
        if(!empty($comments))
        {
            $str = '';
            foreach($comments as $comment){
                $str.='<p style="padding:2px;border-bottom:1px solid #ccc"><b><a href="mailto:'.$comment->email.'">'.$comment->fullname.'</b></a> : '.$comment->comment.'</p>';
            }
            return $str;
        }
        else
            return '<p align=center>ChÆ°a cÃ³ bÃ¬nh luáº­n nÃ o</p>';
    }
}
if(!function_exists('_get_tags')){
    function _get_tags($type=false){
        $ci = &get_instance();
        if(!$type)
            $ci->db->where('tag_type',$type);
        $tags = $ci->db->get('tags')->result();
        $str = "";
        if(!empty($tags)){
            $style='';
            foreach($tags as $tag){
                if($type=='color')
                    $style='style="background-color:'.$tag->name.';"';
                $str .= '<li>'.anchor('tags/'.$tag->tag_type.'/'.$tag->name,$tag->name,'title="'.$tag->name.'"'.$style).'</li>';
            }
        }
        unset($tags);
        return $str;
    }
}
if(!function_exists('_get_fullname')){
    function _get_fullname($user_id){
        $ci = &get_instance();
        $ci->load->model('tank_auth/users','user');
        $profile = $ci->user->get_profile($user_id);
        if(!empty($profile))
        return $profile->name;
        return "";
    }
}
if(!function_exists('_get_size')){
    function _get_size(){
        $str = '<li>'.anchor('tags/size/xs','XS','title="Size XS"').'</li>';
        $str .= '<li>'.anchor('tags/size/s','S','title="Size S"').'</li>';
        $str .= '<li>'.anchor('tags/size/m','M','title="Size M"').'</li>';
        $str .= '<li>'.anchor('tags/size/l','L','title="Size L"').'</li>';
        $str .= '<li>'.anchor('tags/size/xl','XL','title="Size XL"').'</li>';
        $str .= '<li>'.anchor('tags/size/xxl','XXL','title="Size XXL"').'</li>';
        return $str;
    }
}
if(!function_exists('_size')){
    function _size(){
        $str = '<option value="xs">XS</option>';
        $str .= '<option value="s">S</option>';
        $str .= '<option value="m">M</option>';
        $str .= '<option value="l">L</option>';
        $str .= '<option value="xl">XL</option>';
        $str .= '<option value="xxl">XXL</option>';
        return $str;
    }
}
if(!function_exists('_get_slideshow')){
    function _get_slideshow($slidId = false){
        if(!$slidId) show_404();
        $ci=&get_instance();
        $ci->load->model('articles');
            $slides = $ci->articles->get_article_by_category($slidId);    
        
        
        if(!empty($slides)){
            
            $str = '<div class="slider-wrapper theme-default">
            <div class="ribbon"></div><div id="slider" class="nivoSlider">';
                foreach($slides as $slide){
                    $img = $ci->_global->_unserialize($slide->post_content);
                    if(isset($img[$ci->_global->get_default_language()->lang_code])){
                    $img = $img[$ci->_global->get_default_language()->lang_code]['post_content'];
                    $str .= $img;
                    }
                    
                }
            $str.='</div></div>';
            unset($slides);
            return $str;
        }
        return false;
    }
}

if(!function_exists('_get_slideshow2')){
    function _get_slideshow2($cat_id = false){
        
        $ci=&get_instance();
        $slides=_get_feauter_article($cat_id);
        
        if(!empty($slides)){
            $caption = '';
            $str = '<div class="slider-wrapper theme-default">
            <div class="ribbon"></div><div id="slider" class="nivoSlider">';
                $title = null;
                $alias=null;
                $desc=null;
                $img=null;
                foreach($slides as $slide){
                    $title = $ci->_global->_unserialize($slide->post_title);
                    $alias = $ci->_global->_unserialize($slide->seo_alias);
                    $desc = $ci->_global->_unserialize($slide->post_desc);
                    $img = $ci->_global->_unserialize($slide->post_content);
                    
                    if(isset($img[$ci->session->userdata('lan')]))
                        $img = $img[$ci->session->userdata('lan')]['post_content'];
                    else
                        $img = $img[$ci->_global->get_default_language()->lang_code]['post_content'];
                    if(isset($img[$ci->session->userdata('lan')])){
                    $img = _get_thumbnail($img);
                   
                    $str .= '<img title="#htmlcaption'.$slide->post_id.'" src="'.$img.'" width=960 height=390 />';
                   
                    }
                    
                }
            $str.='</div></div>';
            unset($slides);
            return $str;
        }
        return false;
    }
}

if(!function_exists('_get_slideshow3')){
    function _get_slideshow3($cat_id = false){
        $str='';
        $ci=&get_instance();
        $ci->load->model('categories');
        $categories = $ci->categories->getCat(array('cat_type'=>'article'));
        foreach($categories as $cat){
            if($cat->parent_id == $cat_id){
                if($str=='')
                    $str=$cat->cat_id;
                else $str.=','.$cat->cat_id;
                $str.=_get_shop_categories($cat->cat_id);
                
            }
        }
        $sql = 'select * from posts where cat_id in('.$str.') and feature != 0 order by rand() limit 5';
        $query = $ci->db->query($sql);
        $slides = $query->result();
       
        if(!empty($slides)){
            $caption = '';
            $str = '<div class="slider-wrapper theme-default">
            <div class="ribbon"></div><div id="slider" class="nivoSlider">';
                foreach($slides as $slide){
                    $title = $ci->_global->_unserialize($slide->post_title);
                    $alias = $ci->_global->_unserialize($slide->seo_alias);
                    
                    $desc = $ci->_global->_unserialize($slide->post_desc);
                    $img = $ci->_global->_unserialize($slide->post_content);
                    if(isset($img[$ci->_global->get_default_language()->lang_code])){
                    $img = _get_thumbnail($img[$ci->_global->get_default_language()->lang_code]['post_content']);
                    $str .= '<img title="#htmlcaption'.$slide->post_id.'" src="'.$ci->config->item('cdn_url').'exec/timthumb.php?src='.$img.'&w=648&h=308" />';
                    $caption.='<div id="htmlcaption'.$slide->post_id.'" class="nivo-html-caption">
                        <h2 class="title" style="color:#fff">'.$title[$ci->session->userdata('lan')]['post_title'].'</h2>'
                        .substr(strip_tags($desc[$ci->session->userdata('lan')]['post_desc']),0,300).'...'.
                    '
                    <a href="/news/c'.$slide->cat_id.'/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$slide->post_id.'.html" class="btn readmore" style="color:#000">Xem thÃªm</a>
                    </div>';
                    }
                    
                }
            $str.='</div></div>'.$caption;
            unset($slides);
            return $str;
        }
        return false;
    }
}

if(!function_exists('_get_thumbnail')){
    function _get_thumbnail($post)
	{
	   $ci=&get_instance();
	   $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post, $matches);
      if(!empty($matches[1]))
        $first_img = $matches [1][0];
       
      if(empty($first_img)){
        $first_img = 'cdn/frontend/images/no-image.jpg';
      }
      return $first_img;
    }
}
if(!function_exists('_get_all_images')){
    function _get_all_images($post)
	{
	   $ci=&get_instance();
	   $images = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post, $matches);
      if(!empty($matches[1]))
        $images = $matches [1];
       
      if(empty($images)){
        $images = 'cdn/frontend/images/no-image.jpg';
      }
      return $images;
    }
}

if(!function_exists('_get_wishlist')){
    function _get_wishlist()
	{
        $ci=&get_instance();
        $ci->db->where('user_id',$ci->tank_auth->get_user_id());
        return $ci->db->get('wishlist')->result();
    }    
}
if(!function_exists('_get_image_product')){
    function _get_image_product($product_id,$color)
	{
        $ci= &get_instance();
        $ci->db->where('product_id',$product_id);
        $ci->db->select('product_detail');
        $ci->db->from('products');
        $query = $ci->db->get()->row();
        
        $query = $ci->_global->_unserialize($query->product_detail);
        
        if(isset($query['options']['colors'][$color]['images']))
            return $query['options']['colors'][$color]['images'][0];
        else
            return 'cdn/frontend/images/no-image.jpg';
    }
}

if(!function_exists('_get_feauter_article')){
    function _get_feauter_article($cat_id = false)
	{
	   $ci=&get_instance();
       $ci->load->model('articles');
       $feauters = $ci->articles->get_feature_article($cat_id);
       if(!empty($feauters)){
        return $feauters;
               }
       return '';
       
	}
 }

if(!function_exists('_get_feature_product')){
    function _get_feature_product($cat_id = false)
	{
	   $ci=&get_instance();
       $ci->load->model('products');
       
       $features = $ci->products->getProduct(array('is_feature'=>1));
      return $features;
       
	}
 }
 
if(!function_exists('_get_feature_category')){
    function _get_feature_category()
	{
	   $ci=&get_instance();
       $ci->load->model('categories');
       $features = $ci->categories->getCat(array('is_feature'=>1));
       foreach($features as $feature){
        
        $feature->products = _get_product($feature->cat_id,5);
       }
      return $features;
       
	}
 }
 
if(!function_exists('_get_feature_category2')){
    function _get_feature_category2()
	{
	   $ci=&get_instance();
       $ci->load->model('categories');
       $features = $ci->categories->getCat(array('is_feature'=>1));
       foreach($features as $feature){
        $feature->objects = _get_posts($feature->cat_id);
       }
      return $features;
	}
 }
 if(!function_exists('_get_feature_category3')){
    function _get_feature_category3($type=false)
	{
	   $ci=&get_instance();
       $ci->load->model('categories');
       if($type)
            $features = $ci->categories->getCat(array('is_feature'=>1,'cat_type'=>$type));
       else
            $features = $ci->categories->getCat(array('is_feature'=>1));
      return $features;
	}
 }
if(!function_exists('_get_category_parent_id')){
    function _get_category_parent_id($parent_id,$type=true)
	{
	   $ci=&get_instance();
       $ci->load->model('categories');
       if($type)
        $features = $ci->categories->getCat(array('parent_id'=>$parent_id,'cat_type'=>'article'));
        else
        $features = $ci->categories->getCat(array('parent_id'=>$parent_id));
       
      return $features;
       
	}
 }
 if(!function_exists('_get_parent_id')){
    function _get_parent_id($id)
	{
	   $ci=&get_instance();
       $ci->load->model('categories');
       $features = $ci->categories->getCat(array('cat_id'=>$id));
       if(!empty($features)){
       if($features->parent_id<1)
        return $id;
       return $features->parent_id;
        }
        return null;
       
	}
 }
if(!function_exists('_get_banners')){
    function _get_banners($cat_id=false,$limited)
	{
	   $ci=&get_instance();
       $ci->load->model('banners');
       if(!$cat_id)
       $features = $ci->banners->getBanners(array('position'=>'0'),$limited);
       else
       $features = $ci->banners->getBanners(array('position'=>$cat_id),$limited);
       
      return $features;
       
	}
 }
if(!function_exists('_display_object')){
    function _display_object($cat_type,$data)
	{
	   $ci=&get_instance(); 
       $str = '';     
       if($cat_type == 'video'){
            if(!empty($data)){
                $img = $ci->_global->_unserialize($data[0]->post_content);
                if(isset($img[$ci->session->userdata('lan')]))
                    $img = strip_tags($img[$ci->session->userdata('lan')]['post_content']);
                else
                    $img = strip_tags($img[$ci->_global->get_default_language()->lang_code]['post_content']);
            $str.='<div class="image_carousel">';
            $str.='<div id="display_video"><iframe width="300" height="210" src="http://www.youtube.com/embed/'.$img.'" frameborder="0" allowfullscreen></iframe></div>';
            $str.='<div id=foo3>';
            foreach($data as $dt){
                $title = $ci->_global->_unserialize($dt->post_title);
                $img = $ci->_global->_unserialize($dt->post_content);
                if(isset($img[$ci->session->userdata('lan')]))
                    $img = strip_tags($img[$ci->session->userdata('lan')]['post_content']);
                else
                    $img = strip_tags($img[$ci->_global->get_default_language()->lang_code]['post_content']);
                if(isset($title[$ci->session->userdata('lan')]))
                    $title =$title[$ci->session->userdata('lan')]['post_title'];
                else
                    $title =$title[$ci->_global->get_default_language()->lang_code]['post_title'];
                $str.='<img rel="'.$img.'" alt="'.$title.'" src="http://img.youtube.com/vi/'.$img.'/1.jpg" />';
            }
            $str.='</div><div class="clear"></div>
	    <a class="prev" id="foo3_prev" href="#">Pre</a>
	    <a class="next" id="foo3_next" href="#">Next</a></div>';
        }
       }elseif($cat_type=='article')
       {
        if(!empty($data)){
            $str.='<div id=foo2>';
            foreach($data as $dt){
                $title = $ci->_global->_unserialize($dt->post_title);
                $alias = $ci->_global->_unserialize($dt->seo_alias);
                $desc = $ci->_global->_unserialize($dt->post_desc);
                $desc = strip_tags(substr($desc[$ci->session->userdata('lan')]['post_desc'],0,200).'...');
                $img = $ci->_global->_unserialize($dt->post_content);
                $img = _get_thumbnail($img[$ci->session->userdata('lan')]['post_content']);
                $url = "news/c".$dt->cat_id.'/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$dt->post_id.'.html';
                $str.='<div class="slide"><a  href="'.$url.'" title="'.$title[$ci->session->userdata('lan')]['post_title'].'"><img alt="'.$title[$ci->session->userdata('lan')]['post_title'].'" src="'.$ci->config->item('cdn_url').'exec/timthumb.php?src='.$img.'&w=305&h=270" /></a><p><b>'.$title[$ci->session->userdata('lan')]['post_title'].'</b><br/>'.$desc.'<a style="float:right" href="'.$url.'">Xem tiáº¿p</a></p></div>';
            }
            $str.='</div>';
        }
       }elseif($cat_type=='slideshow')
       {
        if(!empty($data)){
            $str.='<div id=foo1>';
            foreach($data as $dt){
                $title = $ci->_global->_unserialize($dt->post_title);
                //$alias = $ci->_global->_unserialize($dt->post_alias);
                $img = $ci->_global->_unserialize($dt->post_content);
                $img = _get_thumbnail($img[$ci->session->userdata('lan')]['post_content']);
                //var_dump($img);
                $str.='<div class="slide"><a rel="fancybox" href="'.$img.'"><img src="'.$ci->config->item('cdn_url').'exec/timthumb.php?src='.$img.'&w=305&h=270" alt="'.$title[$ci->session->userdata('lan')]['post_title'].'" /></a><h4>'.$title[$ci->session->userdata('lan')]['post_title'].'</h4></div>';
            }
            $str.='</div>';
        }
       }elseif($cat_type=='gallery') {
        if(!empty($data)){
            $str.='<div id=foo2 style="position:relative">';
            foreach($data as $dt){
                $title = $ci->_global->_unserialize($dt->post_title);
                $alias = $ci->_global->_unserialize($dt->seo_alias);
                $img = $ci->_global->_unserialize($dt->post_desc);
                $img = _get_thumbnail($img[$ci->session->userdata('lan')]['post_desc']);
                $url = "gallery/c".$dt->cat_id.'/'.$alias[$ci->session->userdata('lan')]['alias'].'-'.$dt->post_id.'.html';
                $str.='<div class="slide"><a  href="'.$url.'" title="'.$title[$ci->session->userdata('lan')]['post_title'].'"><img alt="'.$title[$ci->session->userdata('lan')]['post_title'].'" src="'.$ci->config->item('cdn_url').'exec/timthumb.php?src='.$img.'&w=305&h=270" /></a><p><b>'.$title[$ci->session->userdata('lan')]['post_title'].'</b><a style="float:right" href="'.$url.'">Xem tiáº¿p</a></p></div>';
            }
            $str.='</div>';
        }
       }
       return $str;
	}
    
 }
  
if(!function_exists('_set_alias')){
    function _set_alias($data = '')
	{
	   if(empty($data))
            return false;
	     $ci = &get_instance();
        $marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
		"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
		,"ế","ệ","ể","ễ",
		"ì","í","ị","ỉ","ĩ",
		"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
		,"ờ","ớ","ợ","ở","ỡ",
		"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
		"ỳ","ý","ỵ","ỷ","ỹ",
		"đ",
		"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
		,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
		"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
		"Ì","Í","Ị","Ỉ","Ĩ",
		"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
		,"Ờ","Ớ","Ợ","Ở","Ỡ",
		"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
		"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
		"Đ","&",".",",","'",'"','“','”','%','!','@','#','^','&',"$",'*','(',')','?');	
		$marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
		,"a","a","a","a","a","a",
		"e","e","e","e","e","e","e","e","e","e","e",
		"i","i","i","i","i",
		"o","o","o","o","o","o","o","o","o","o","o","o"
		,"o","o","o","o","o",
		"u","u","u","u","u","u","u","u","u","u","u",
		"y","y","y","y","y",
		"d",
		"A","A","A","A","A","A","A","A","A","A","A","A"
		,"A","A","A","A","A",
		"E","E","E","E","E","E","E","E","E","E","E",
		"I","I","I","I","I",
		"O","O","O","O","O","O","O","O","O","O","O","O"
		,"O","O","O","O","O",
		"U","U","U","U","U","U","U","U","U","U","U",
		"Y","Y","Y","Y","Y",
		"D","","","","","","","","","","","","","","","","","");
		$data = str_replace($marTViet, $marKoDau, $data);
		
		return strtolower( str_replace(' ','-',$data) );
    }}
    
    
